package com.itxx.homeWork.demo4;

import com.itxx.homeWork.Student1;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Client {
    /*
  升级版: 加上Socket
  客户端 ->
  新建一个类Student -> String name,Integer chineseScore,Integer mathScore,Integer englishScore.
  [GET/SET/有参/无参/toString/equals]
  使用键盘录入三个学生的数据 -> 12次,通过反射封装三个学生对象.
  存储到ArrayList中,将ArrayList中的数据按照主要条件:总分 次要条件:语文 次要:数学进行排序(升序/降序无所谓) 默认所有学生均不重复.
  将ArrayList序列化到当前模块下的student.txt文件中,然后将student.txt文件上传到服务器端.

  服务器端:
  接收到这个数据之后,进行反序列化,将学生排序信息打印到控制台.
  */
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, IOException {
        Socket socket = new Socket("127.0.0.1", 54564);

        Scanner sc = new Scanner(System.in);
        ArrayList<Student1> list = new ArrayList<Student1>();
        list.sort(new Comparator<Student1>() {
            @Override
            public int compare(Student1 s1, Student1 t1) {
                //总分 次要条件:语文 次要:数学进行排序(升序/降序无所谓)
                //主要条件  总分
                int result = (t1.getChineseScore() + t1.getEnglishScore() + t1.getMathScore()) - (s1.getChineseScore() + s1.getEnglishScore() + s1.getMathScore());
                if (result == 0) {
                    result = s1.getChineseScore() - t1.getChineseScore();
                } else if (result == 0) {
                    result = s1.getMathScore() - t1.getMathScore();
                }
                return result;
            }
        });
        //创建Class对象
        Class aClass = Class.forName("com.itxx.homeWork.Student1");
        //有参构造
        Constructor constructor = aClass.getDeclaredConstructor(String.class, Integer.class, Integer.class, Integer.class);
        constructor.setAccessible(true);

        //创建一个序列化流
        ObjectOutputStream obs = new ObjectOutputStream(new FileOutputStream("student.txt"));
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入" + (i + 1) + "个学生的姓名");
            String s = sc.next();
            System.out.println("请输入" + (i + 1) + "个学生的语文成绩");
            int ii = sc.nextInt();
            System.out.println("请输入" + (i + 1) + "个学生的数学成绩");
            int ii2 = sc.nextInt();
            System.out.println("请输入" + (i + 1) + "个学生的英语成绩");
            int ii3 = sc.nextInt();
            Student1 stu = (Student1) constructor.newInstance(s, ii, ii2, ii3);
            int sum = ii + ii2 + ii3;
            list.add(stu);
            obs.writeObject(stu);

        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("student.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        int len;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);

        }

        socket.close();
        bis.close();

        obs.close();
    }
}
